enterpriseObj = {
  init() {
    enterpriseObj.jumpPage();
    enterpriseObj.addEnterpriseData();
    enterpriseObj.getData();
    enterpriseObj.search();
    this.enterpriseList = [];
  },
  jumpPage() {
    $(document).ready(function () {
      $(".back").click(function () {
        history.back();
      });
    });
  },
  addEnterpriseData() {
    url = "http://localhost:3000/enterprise";
    axios.get(url).then((res) => {
      enterpriseObj.renderEnterpriseList(res.data);
      this.enterpriseList = res.data; // 保存数据
    });
  },
  enterpriseList(list) {
    var htmlStr = "";
    list.forEach(function (item) {
      htmlStr += `
        <a href="../company-details/company-details.html?id=${item.id}" class="item bg-fff flex pt-20 pb-20 pl-15 pr-15 lh15 ">
            <img src="${item.imgUrl}" alt="">
            <div class="text ml-15 bb2 w100p pb-20">
                <p class=" f14"><span>${item.companyName}</span><span class="sc f10 ml-10">认证</span></p>
                <p class=" f10 c888"> <span>${item.peopleNum}</span>|<span>${item.workType}</span></p>
                <p class=" f12 welfare c888"><span >${item.welfare}</span><span class="ml-10">${item.welfare2}</span><span class="ml-10">${item.welfare3}</span></p>
                <p class=" f12 flex aic"><span>热招：</span><span class="base move">${item.hotMove}</span> <span>在招${item.positionNum}个职位</span><i class="iconfont icon-youjiantou"></i></p>
            </div>
        </a>
            `;
      $(".list").html(htmlStr);
    });
  },
  // 搜索
  search() {
    $("#submit").click(function () {
      var searchText = $("#searchText").val();
      if (searchText == "") {
        alert("请输入搜索内容");
      } else {
        var url = `http://localhost:3000/enterprise?q=${searchText}`;
        axios
          .get(url)
          .then(function (res) {
            enterpriseObj.render(res.data);
          })
          .catch(function (err) {
            console.log("查询失败", err);
          });
      }
    });
  },
  render(list) {
    var htmlStr = "";
    list.forEach((item) => {
      htmlStr += `
        <a href="../company-details/company-details.html?id=${item.id}" class="item bg-fff flex pt-20 pb-20 pl-15 pr-15 lh15 ">
          <img src="${item.imgUrl}" alt="">
          <div class="text ml-15 bb2 w100p pb-20">
              <p class=" f14"><span>${item.companyName}</span><span class="sc f10 ml-10">认证</span></p>
              <p class=" f10 c888"> <span>${item.peopleNum}</span>|<span>${item.workType}</span></p>
              <p class=" f12 welfare c888"><span >${item.welfare}</span><span class="ml-10">${item.welfare2}</span><span class="ml-10">${item.welfare3}</span></p>
              <p class=" f12 flex aic"><span>热招：</span><span class="base move">${item.hotMove}</span> <span>在招${item.positionNum}个职位</span><i class="iconfont icon-youjiantou"></i></p>
          </div>
        </a>
      `;
    });
    $(".list").html(htmlStr);
  },
  getData() {
    $("select").change(() => {
      // 获取选中的option的下标
      var selectedIndex1 = $(".address").find("option:selected").index();
      var selectedIndex2 = $(".welfare").find("option:selected").index();
      var selectedIndex3 = $(".worktype").find("option:selected").index();
      var address = $(".address").find("option").eq(selectedIndex1).text();
      var welfare = $(".welfare").find("option").eq(selectedIndex2).text();
      var worktype = $(".worktype").find("option").eq(selectedIndex3).text();

      // 构建查询参数
      var list = [];
      if (address !== "所在区域" && address !== "其他") {
        list = this.enterpriseList.filter((item) => {
          return item.address.includes(address);
        });
      } else {
        list = this.enterpriseList;
      }

      if (address == "其他") {
        list = list.filter((item) => {
          return (
            !item.address.includes("西乡塘") &&
            !item.address.includes("青秀区") &&
            !item.address.includes("江南区") &&
            !item.address.includes("良庆区") &&
            !item.address.includes("邕宁区") &&
            !item.address.includes("高新区")
          );
        });
      }

      if (worktype !== "行业分类" && worktype!== "其他") {
        list = list.filter((item) => {
          return item.workType.includes(worktype);
        });
      }
      if (worktype == "其他") {
        list = list.filter((item) => {
          return (
            !item.workType.includes("建筑") &&
            !item.workType.includes("制造业") &&
            !item.workType.includes("汽车") &&
            !item.workType.includes("计算机") &&
            !item.workType.includes("餐饮") &&
            !item.workType.includes("日用品") &&
            !item.workType.includes("新闻") &&
            !item.workType.includes("广告") &&
            !item.workType.includes("日常服务") &&
            !item.workType.includes("财险") &&
            !item.workType.includes("保健品")
          );
        });
      }
      if (welfare !== "企业福利" && welfare !== "其他") {
        list = list.filter((item) => {
          return (
            item.welfare.includes(welfare) ||
            item.welfare2.includes(welfare) ||
            item.welfare3.includes(welfare)
          );
        });
      }

      if (welfare == "其他") {
        list = list.filter((item) => {
          return (
            !item.welfare.includes("住房公积金") &&
            !item.welfare.includes("五险") &&
            !item.welfare.includes("五险一金") &&
            !item.welfare.includes("包吃住") &&
            !item.welfare.includes("包吃") &&
            !item.welfare.includes("包住") &&
            !item.welfare.includes("双休") &&
            !item.welfare.includes("月休6天") &&
            !item.welfare.includes("工作餐") &&
            !item.welfare.includes("房补") &&
            !item.welfare.includes("餐补") &&
            !item.welfare.includes("油补") &&
            !item.welfare.includes("交通补") &&
            !item.welfare.includes("多劳多得") &&
            !item.welfare.includes("高薪资") &&
            !item.welfare.includes("压力小") &&
            !item.welfare.includes("免费培训")
          );
        });
      }
      enterpriseObj.renderEnterpriseList(list);
      
    });
   
  },

  // 合并处理函数
  renderEnterpriseList(list) {
    var htmlStr = "";
    list.forEach((item) => {
      htmlStr += `
        <a href="../company-details/company-details.html?id=${item.id}" class="item bg-fff flex pt-20 pb-20 pl-15 pr-15 lh15 ">
          <img src="${item.imgUrl}" alt="">
          <div class="text ml-15 bb2 w100p pb-20">
              <p class=" mt-5 f14"><span>${item.companyName}</span><span class="sc f10 ml-10">认证</span></p>
              <p class=" mt-5 f10 c888"> <span class="mr-10">${item.peopleNum}人</span>|<span class=" ml-10">${item.workType}</span></p>
              <p class=" mt-5 f12 welfare c888"><span >${item.welfare}</span><span class="ml-10">${item.welfare2}</span><span class="ml-10">${item.welfare3}</span></p>
              <p class=" mt-5 f12 flex aic"><span>热招：</span><span class="base move">${item.hotMove}</span><span>等职位</span></p>
          </div>
        </a>
      `;
    });
    $(".list").html(htmlStr);
  },
};
enterpriseObj.init();
